using UnityEngine;
using QFramework;
using UnityEngine.Video;

namespace ProjectSurvivor
{
	public partial class Ball : ViewController
	{
		public BindableProperty<bool> SupperBaskeBall = new BindableProperty<bool>(false);

		void Start()
		{
			SupperBaskeBall.RegisterWithInitValue(unlocked=>
			{
				if (unlocked)
				{
					this.LocalScale(9);
				}
			});
			SelfRigidbody2D.velocity = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)) * Random.Range(Global.Bollspeed.Value - 2, Global.Bollspeed.Value + 2);
			HurtBox.OnTriggerEnter2DEvent(collider =>
			{
				var hurtBox = collider.GetComponent<HurtBox>();
				if (hurtBox)
				{
					if (hurtBox.Owner.CompareTag("Enemy"))
					{
						var damageTimes = SupperBaskeBall.Value ? Random.Range(2, 3 + 1) :1;
						if (hurtBox.Owner.GetComponent<Enemy>())
						{
							DamageSystem.CalculateDamage(Global.SimpleAbilityDamage.Value * damageTimes, hurtBox.Owner.GetComponent<Enemy>());
						}
						else
						{
							DamageSystem.CalculateDamage(Global.SimpleAbilityDamage.Value * damageTimes, hurtBox.Owner.GetComponent<EnemyMinBoss>());
						}
						
						if (Random.Range(0, 1f) < 0.5f&&collider&&collider.attachedRigidbody&&Player.Default)
						{
							collider.attachedRigidbody.velocity = collider.NormalizedDirection2DFrom(this)*5+collider.NormalizedDirection2DFrom(Player.Default)*10;
						}
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}
		void OnCollisionEnter2D(Collision2D collision)
		{
			if (Global.SimpleSwordCount.Value > 10)
			{
				SupperBaskeBall.Value = true;
			}
			var normal = collision.GetContact(0).normal;
			AudioKit.PlaySound("ball");
			if (normal.x > normal.y)
			{
				SelfRigidbody2D.velocity = new Vector2(SelfRigidbody2D.velocity.x,
					 Mathf.Sign(SelfRigidbody2D.velocity.y) * Random.Range(0.5f, 1.5f) *
					Random.Range(Global.Bollspeed.Value - 2, Global.Bollspeed.Value + 2));
				SelfRigidbody2D.angularVelocity = Random.Range(-360, 360);
			}
			else
			{
				var rb = SelfRigidbody2D;
				rb.velocity =
					new Vector2(
						Mathf.Sign(rb.velocity.x) * Random.Range(0.5f, 1.5f) * Random.Range(
							Global.Bollspeed.Value - 2, Global.Bollspeed.Value + 2),
							rb.velocity.y);

				rb.angularVelocity = Random.Range(-360, 360);

			}
		}
    }
}
